import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 晓星航
 * Date: 2023-03-18
 * Time: 18:46
/* *//*class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(char val) {
        this.val = val;
    }
}
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static int i = 0;
    public static TreeNode createTree(String str) {
        TreeNode root = null;
        if (str.charAt(i) != '#') {
            root = new TreeNode(str.charAt(i));
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        } else {
            //遇到# 就是空树
            i++;
        }
        return root;
    }
    public static void inorder(TreeNode root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            TreeNode root = createTree(str);
            inorder(root);
        }
    }
}*/
class BTNode {
    public char val;
    public BTNode left;//左孩子的引用
    public BTNode right;//右孩子的引用
    public BTNode(char val) {
        this.val = val;
    }
}

public class BinaryTree {
    public BTNode root;//二叉树的根结点

    /**
     * 这种方式 是穷举的方式创建的
     * 但是 这样写太low了
     * @return
     */
    public BTNode creatTree (){
        BTNode A = new BTNode('A');
        BTNode B = new BTNode('B');
        BTNode C = new BTNode('C');
        BTNode D = new BTNode('D');
        BTNode E = new BTNode('E');
        BTNode F = new BTNode('F');
        BTNode G = new BTNode('G');
        BTNode H = new BTNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
//        E.right = H;
        C.left = F;
        C.right = G;
        return A;
    }
    //前序遍历法
    void preOrder(BTNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历法
    void inOrder(BTNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    //后续遍历法
    void lastOrder(BTNode root) {
        if (root == null) {
            return;
        }
        lastOrder(root.left);
        lastOrder(root.right);
        System.out.print(root.val + " ");
    }
    int count = 0;
    int size1(BTNode root) {
        if (root == null) {
            return 0;
        }
        count++;
        size1(root.left);
        size1(root.right);
        return count;
    }
    int size2(BTNode root) {
        if (root == null) {
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }
    int leafCount = 0;
    int getLeafNodeCount(BTNode root) {
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        } else {
            return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
        }
    }
    void getLeafNodeCount1(BTNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            leafCount++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }
    int getLeafNodeCount2(BTNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    int getLeverNodeCount(BTNode root,int k) {
        if (root == null || k <= 0) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getLeverNodeCount(root.left,k - 1) + getLeverNodeCount(root.right,k - 1);
    }
    /**
     * 时间复杂度：O()
     * 获取二叉树的高度
     */
    int getHeight(BTNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    }
    BTNode find(BTNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        BTNode ret = find(root.left,val);
        if (ret != null) {
            return ret;
        }
        ret = find(root.right,val);
        if (ret != null) {
            return ret;
        }
        return null;
    }
    boolean isCompleteTree(BTNode root) {
        if (root == null) {
            return true;
        }
        Queue<BTNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            BTNode cur = queue.poll();
            if (cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            } else {
                break;
            }
        }
        while (!queue.isEmpty()) {
            BTNode top = queue.peek();
            if (top != null) {
                return false;
            }
            queue.poll();
        }
        return true;
    }

    /**
     * 时间复杂度是:O(min(M,N))
     * @param p
     * @param q
     * @return
     */
    public boolean isSameTree(BTNode p, BTNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null && q != null || p != null && q == null) {
            return false;
        }
        if (p.val != q.val) {
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }

    /**
     * 时间复杂度:O(m * n)
     * @param root
     * @param subRoot
     * @return
     */
    public boolean isSubtree(BTNode root,BTNode subRoot) {
        if (root == null || subRoot == null) {
            return false;
        }
        //根节点和subRoot是不是两颗相同的树
        if (isSameTree(root,subRoot)) {
            return true;
        }
        //subRoot是不是root的左子树
        if (isSubtree(root.left,subRoot)) {
            return true;
        }
        //subRoot是不是root的右子树
        if (isSubtree(root.right,subRoot)) {
            return true;
        }
        return false;
    }

    /**
     *
     * @param root
     * @return
     */
    public boolean isBalanced(BTNode root) {
        if (root == null) {
            return true;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        return Math.abs(left - right) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }
    public boolean isSymmetricChild(BTNode leftTree,BTNode rightTree) {
        if (leftTree == null && rightTree != null ||leftTree != null && rightTree == null) {
            return false;
        }
        if (leftTree == null && rightTree == null) {
            return true;
        }
        if (leftTree.val != rightTree.val) {
            return false;
        }
        return isSymmetricChild(leftTree.left,rightTree.right) && isSymmetricChild(leftTree.right,rightTree.left);
    }
    public boolean isSymmetric(BTNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetricChild(root.left,root.right);
    }



}

